/*
 * Copyright (C) 2012 Google Inc.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Controls the anymoteSession object.  The anymoteSession object is created by
 * the googletvremote plugin during initialization.  The Anymote session object
 * is the communication mechanism with a paired device.  Before you can use the
 * Anymote session you need to pair with a Google TV device using a Pairing
 * session.  Anymote session functions include:
 *     startSession(name, version, host, port, callback)
 *     stopSession()
 *     sendKeyEvent(keycode, action)
 *     sendMouseMove(xDelta, yDelta)
 *     sendMouseWheel(xScroll, yScroll)
 *     sendDatafunction(type, data)
 *     sendFling(uri, sequence)
 *     sendPing()
 */


/** Anymote session object that this controller manages. */
var anymoteSession;


/** Array of ip addresses loaded from localStorage that have already paired. */
var pairedDevices = [];


/** Index used when traversing through the list of paired devices. */
var deviceIndex = 0;


/**
 * Start an Anymote session to communicate with Google TV. Before starting an
 * Anymote session, the Google TV at this ip address must already be paired.
 * @param {string} googleTvIpAddress IP address of the Google TV.
 * @param {function} response Callback function that will be called when the
 *     starting the Anymote session is successful or fails.
 */
var anymoteStartSession = function(googleTvIpAddress, response) {
  displayToast('anymoteStartSession not implemented');
  // TODO: Set a timeout that will trigger if there is no reply from the TV.
  // TODO: Stop the old session if one is already running.
  // TODO: Start an Anymote session.
  // TODO: Listen for Anymote events from the TV.  For example,
  //  googletvremote.anymote.EventType.CONNECTED save the device
  //  googletvremote.anymote.EventType.ACK print a message
  //  googletvremote.anymote.EventType.ERROR send error response to callback
};


/** Go through the paired device list and attempt to start Anymote sessions. */
var anymoteConnectToExistingDevice = function() {
  displayToast('anymoteConnectToExistingDevice not implemented');
  // TODO: Pull the paired device list from localStorage
  // TODO: Attempt to pair with the device in index 0
  // TODO: Set a callback that will try the next device if this one fails.
};


/**
 * Calls the next device in the list if this device failed.
 * @param {Object} e Event indicating if the start session event was successful.
 */
var anymoteExistingDeviceResponseHandler = function(e) {
  displayToast('anymoteExistingDeviceResponseHandler not implemented');
  // If the type is googletvremote.anymote.EventType.CONNECTED you are done.
  // TODO: Otherwise attempt to connect using the next device in the array.
};


/**
 * Sends an Anymote key event to Google TV.  See the googletvremote_constants
 * for a complete list of keycode values.
 * @param {number} keycode Anymote keycode value to send to the TV.
 */
var sendAnymoteKeyEvent = function(keycode) {
  anymoteSession.sendKeyEvent(keycode, googletvremote.anymote.Action.DOWN);
  anymoteSession.sendKeyEvent(keycode, googletvremote.anymote.Action.UP);
};


/**
 * Sends a data message of type STRING.
 * @param {string} message String to send to the TV.
 */
var sendAnymoteStringMessage = function(message) {
  anymoteSession.sendData(googletvremote.anymote.DataType.STRING, message);
};


/**
 * Flings a uri to the Google TV.
 * @param {string} uri Uri to send to the Google TV.  This uri will be used as
 *     the data property in an Android Intent sent to Google TV.
 * @see http://developer.android.com/guide/topics/intents/intents-filters.html
 * @param {number=} sequenceNumber Optional sequence number that will be used in
 *     the Google TV reply to this message.  Not critical but must be > 0.
 */
var sendAnymoteFling = function(uri, sequenceNumber) {
  sequenceNumber = sequenceNumber || 42; // Sequence number default if not given
  anymoteSession.sendFling(uri, sequenceNumber);
};


/** Sends a ping to the Google TV. Used to test connection status. */
var sendAnymotePing = function() {
  anymoteSession.sendPing();
};
